home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1640 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.5 KB  |  60 lines

  1. Path: news.clark.net!not-for-mail
  2. From: gusty@clark.net (Harlan Messinger)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: cout stupidity
  5. Date: 11 Jan 1996 20:13:36 GMT
  6. Organization: Clark Internet Services, Inc., Ellicott City, MD USA
  7. Distribution: world
  8. Message-ID: <4d3r1g$uj@clarknet.clark.net>
  9. References: <4d321g$eal@calvin.st-and.ac.uk>
  10. NNTP-Posting-Host: explorer.clark.net
  11. Mime-Version: 1.0
  12. Content-Type: TEXT/PLAIN; charset=ISO-8859-1
  13. Content-Transfer-Encoding: 8bit
  14. X-Newsreader: TIN [UNIX 1.3 950726BETA PL0]
  15.  
  16. Keith Sibson (ks2@st-and.ac.uk) wrote:
  17. : #include <iostream.h>
  18. : int count=10;
  19. : main()
  20. : {
  21. :  for(i=0;i<cout;i++) {.....}
  22. :  return(0);
  23. : }
  24. : Why does this stupidity compile? (BCC 4.5)
  25. : Surely cout is of type ostream? Either there is a dubious operator< that 
  26. : takes an int and ostream, or there is some wacky automatic conversions going
  27. : on. Does an ostream cast to an int that is the stream position?
  28. : Keith.
  29.  
  30. An overload in the ios base class appears to be the source of this. In 
  31. VC++'s ios.h, we have
  32.  
  33.     operator void *() const { if(state&(badbit|failbit) ) 
  34.         return 0; return (void *)this; }
  35.  
  36. I guess this allows an ios object to be implicitly recast as a void 
  37. pointer, to the object if it's valid or to NULL otherwise. This allows 
  38. one to use the object iself as the control expression in if statements 
  39. and so forth:
  40.  
  41.     if (my_ios) { ... } /* Execute this code only if my_ios is valid */
  42.  
  43. They also overload the ! operator,
  44.  
  45.     inline int ios::operator!() const { return state&(badbit|failbit); }
  46.  
  47. allowing 
  48.  
  49.     if (!my_ios) { ... }
  50.  
  51.  
  52.  
  53.